﻿Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.Services
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.PersistentStorage
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.Administration.DomainModels

Namespace Services
    Public Class UserService
        Inherits ServiceBase
        Implements IUserService

        Public Function AuthenticateUser(username As String, password As String) As SouthdaleUser Implements IUserService.AuthenticateUser
            StatementSource = New AuthenticateUserStatementSource(username, password)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Public Sub Create(employeeId As Integer, username As String, password As String, createdBy As Integer) Implements IUserService.Create
            StatementSource = New CreateUserStatementSource(employeeId, username, password, createdBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Delete(userId As Integer) Implements IUserService.Delete
            StatementSource = New DeleteUserStatementSource(userId)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Edit(entity As SouthdaleUser, modifiedBy As Integer) Implements IUserService.Edit
            StatementSource = New UpdateUserStatementSource(entity.UserId, entity.Employee.EmployeeId, entity.Username, entity.Password, modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAll() As IEnumerable(Of SouthdaleUser) Implements IUserService.ReadAll
            StatementSource = New ReadAllUserStatementSource()
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadByUserId(userId As Integer) As SouthdaleUser Implements IUserService.ReadByUserId
            StatementSource = New ReadByUserIdStatementSource(userId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Private Function MapDataToEntity(dataReader As SqlDataReader) As IEnumerable(Of SouthdaleUser)
            Dim entities As New List(Of SouthdaleUser)

            If (dataReader.HasRows) Then
                Dim employeeService As IEmployeeService = New EmployeeService

                While (dataReader.Read())
                    Dim entity As New SouthdaleUser()
                    Dim employeeId As Integer = Convert.ToInt32(dataReader("EmployeeId"))

                    entity.Password = Convert.ToString(dataReader("Password"))
                    entity.Username = Convert.ToString(dataReader("Username"))
                    entity.UserId = Convert.ToInt32(dataReader("UserId"))
                    entity.Employee = employeeService.ReadById(employeeId)

                    entities.Add(entity)
                End While
            End If

            Return entities
        End Function

    End Class
End Namespace
